#!/bin/bash

SH_BASE=`dirname $0`/../
RPC=`find $SH_BASE -name rpc.exp`
SCP=`find $SH_BASE -name scp.exp`
PROG=`basename $0`

DHCP_DAT="/tmp/dhcp.dat"
PING_DAT="/tmp/ping.dat"
SRV_LIST="/tmp/.$PROG.servers.list"

function get_dhcp_config()
{
    DHCP_CONF=dhcp.test

    echo "Geting DHCP configure file from DHCP server ......"

    $SCP lance lance@10.0.0.1:/etc/dhcp3/$DHCP_CONF ./
    cat $DHCP_CONF | awk '{if (!($1 ~ /^#/)) \
    {\
        if ( $1 == "host") { gsub(/\{/, "", $2); printf("%s\t", $2) }\
        if ( $1 == "hardware" && $2 == "ethernet" ) { gsub(/;/, "", $3);  printf("%s\t", $3)}\
        if ( $1 == "fixed-address" ) { gsub(/;/, "", $2);  printf("%s\t", $2)}\
        if ( $1 ~ /\}/) { print "" }\
    }}' > $DHCP_DAT
    rm -f $DHCP_CONF

    echo "Retrieve ["`cat $DHCP_DAT | wc -l`"] Servers from DHCP config file."
    echo ""
}

function broadcasting_search()
{
    PING_LOG="/tmp/ping.log"

    rm -f $PING_DAT

    echo "Using broadcasting to find more server ......"
    ping -c 10 -b 10.255.255.255 1>$PING_LOG 2>&1 

    cat $PING_LOG | awk '($4 ~ /:$/){ print substr($4, 0, length($4)-1)}' | sort -u | while read ipaddr
    do
        host_text=`host $ipaddr`
        host_name=`echo $host_text | awk '( $(NF-1) != "found:"){ print substr($NF, 0, length($NF)-1) }'`
        host_addr="$ipaddr"
        [ ! -z "$host_name" ] && host_mac=`arp -a "$host_name" 2>/dev/null | awk '($3 ~ /at/){print $4}'`
        echo $host_name,$host_addr,$host_mac >> $PING_DAT
    done
    rm -f $PING_LOG

    echo "Retrieve ["`cat $PING_DAT | wc -l`"] Servers from broadcasting."
    echo ""
}

function join_result()
{
    echo "Combining server list ......"
    cp $DHCP_DAT $SRV_LIST
    #Merge data
    cat $PING_DAT | awk -F ',' '($1 != ""){ print $1, $2, $3}' | while read name addr mac
    do
        DUPLICATE="0"
        [ ! -z "$mac" ] && grep -i "$mac" $DHCP_DAT 2>/dev/null 1>&2 && DUPLICATE="1"
        [ ! -z "$name" ] && grep -i "$name" $DHCP_DAT 2>/dev/null 1>&2 && DUPLICATE="1"
        [ ! -z "$addr" ] && grep -i "$addr" $DHCP_DAT 2>/dev/null 1>&2 && DUPLICATE="1"
        if  [ $DUPLICATE == "0" ]
        then
            echo -e "$name\t$mac\t$addr" >> $SRV_LIST
        fi
    done

    rm -f $PING_DAT
    rm -f $DHCP_DAT
}

function check_server()
{
    echo "Starting verify all servers ......"
    cat $SRV_LIST | while read name mac ipaddr
    do
        result="/tmp/.$PROG.$name.cache"
        if [ ! -z "$name" ]
        then
            nohup ping -c 1 "$name" -q 1>$result 2>&1 &
        fi
    done
    echo "Verification command sent, waiting for server response ......"
}

function read_result()
{
    SEP="--------------------------------------------------------------"
    echo "" | awk '{printf("%25s %15s %20s\n", "Host Name", "IP Address", "Mac Address")}'
    echo $SEP
    cat $SRV_LIST | while read name mac ipaddr
    do
        result="/tmp/.$PROG.$name.cache"
        if [ ! -z "$name" -a -f "$result" ]
        then
            cat "$result" | grep "rtt min/avg/max/mdev =" 1>/dev/null 2>&1
            if [ "$?" == 0 ]
            then
                host_text=`host $name`
                host_name=`echo $host_text | awk '{print $1}'`
                host_addr=`echo $host_text | awk -F'address' '{print $2}'`
                host_mac=`arp -a "$host_name" | awk '($3 ~ /at/){print $4}'`
                echo $host_name,$host_addr,$host_mac | awk -F',' '{printf("%25s %15s %20s\n", $1, $2, $3)}'
            fi
            #rm -f "$result"
        fi
    done
}

if [ ! -f "$SRV_LIST" ]
then
    echo "Server List is missing, re-caching all servers ......"
    get_dhcp_config
    broadcasting_search
    join_result
fi

check_server
sleep 10
read_result

exit 0
